var customerProtocolChecked = true;
var errorCustomerId = true;
var errorCustomerPassword = true;
var errorCustomerConfirmPwd = true;
var errorCustomerEmail = true;

$(document).ready(function(){
	$("#customer_id").focus(function(){
		$("#customer_id").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	});
	$("#customer_id").blur(function(){
		if ($("#customer_id").val() == "") {
			$("#customer_id").css("background", "url('" + contextPath + "/image/bg_text_uid.png')");
		}
	});
	

	$("#customer_email").focus(function(){
		$("#customer_email").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	});
	$("#customer_email").blur(function(){
		if ($("#customer_email").val() == "") {
			$("#customer_email").css("background", "url('" + contextPath + "/image/bg_text_email.png')");
		}
	});
	

	$("#customer_password").focus(function(){
		$("#customer_password").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	});
	$("#customer_password").blur(function(){
		if ($("#customer_password").val() == "") {
			$("#customer_password").css("background", "url('" + contextPath + "/image/bg_text_password.png')");
		}
	});
	

	$("#customer_confirm_pwd").focus(function(){
		$("#customer_confirm_pwd").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	});
	$("#customer_confirm_pwd").blur(function(){
		if ($("#customer_confirm_pwd").val() == "") {
			$("#customer_confirm_pwd").css("background", "url('" + contextPath + "/image/bg_text_confirm_pwd.png')");
		}
	});
	
	addFadeEffect();
});

function toggleCustomerProtocolRadio() {
	customerProtocolChecked = !customerProtocolChecked;
	
	if (customerProtocolChecked) {
		$("#customer_protocol_radio").css("background", "url('" + contextPath + "/image/radio_gray_selected.png')");
	} else {
		$("#customer_protocol_radio").css("background", "url('" + contextPath + "/image/radio_gray_unselected.png')");
	}
}

function customerRegister() {
	var customerId = $("#customer_id").val();
	var email = $("#customer_email").val();
	var password = $("#customer_password").val();
	var confirmPwd = $("#customer_confirm_pwd").val();
	var requestUrl = contextPath + "/customer/register";
	var params;
	
	if (checkCustomerId(customerId) && !errorCustomerId
			&& checkCustomerPassword(password) && !errorCustomerPassword
			&& checkCustomerEmail(email) && !errorCustomerEmail
			&& checkCustomerConfirmPwd(confirmPwd) && !errorCustomerConfirmPwd
			&& checkCustomerAgreement()) {

		params = {
				customer_id:customerId,
				password:password,
				email:email
		};
		
		$.post(requestUrl, params, function(ret){
			if (ret.code == 0) {
				showCustomerRegisterSuccess();
			} else {
				$("#customer_register_error").html(ret.message);
			}
		}, "json");
	}
}

function cancelCustomerRegister() {
	$("#div_customer_register").remove();
	$(cover).remove();
}

function checkCustomerId(customerId) {
	var pattern = /[a-zA-Z0-9]{4,20}/;
	
	$("#customer_register_error").html("");
	$("#customer_id").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	errorCustomerId = false;
	if (customerId == "" || !pattern.test(customerId)) {
		$("#customer_register_error").html("用户名错误<br/>");
		$("#customer_id").css("background", "#fbe2e2");
		errorCustomerId = true;
	} else {
		requestUrl = "check_user_id";
		$.post(requestUrl, {user_id:customerId}, function handlerCheckUserId(ret) {
			if (ret != "true") {
				$("#customer_register_error").html("用户名已存在<br/>");
				$("#customer_id").css("background", "#fbe2e2");
				errorCustomerId = true;
			} else {
				errorCustomerId = false;
			}
		});
	}
	return true;
}

function checkCustomerPassword(password) {
	var pattern = /[a-zA-Z0-9]{4,20}/;
	
	if (password == "" || !pattern.test(password)) {
		$("#customer_register_error").html("密码错误<br/>");
		$("#customer_password").css("background", "#fbe2e2");
		errorCustomerPassword = true;
	} else {
		$("#customer_register_error").html("");
		$("#customer_password").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
		errorCustomerPassword = false;
	}
	return true;
}

function checkCustomerConfirmPwd(confirmPwd) {
	if ($("#customer_password").val() != confirmPwd) {
		$("#customer_register_error").html("确认密码错误<br/>");
		$("#customer_confirm_pwd").css("background", "#fbe2e2");
		errorCustomerConfirmPwd = true;
	} else {
		$("#customer_register_error").html("");
		$("#customer_confirm_pwd").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
		errorCustomerConfirmPwd = false;
	}
	return true;
}

function checkCustomerEmail(email) {
	var pattern = /^[^\s@]+@[^\s@]+$/;
	
	if (email == "" || !pattern.test(email)) {
		$("#customer_register_error").html("邮箱错误<br/>");
		$("#customer_email").css("background", "#fbe2e2");
		errorCustomerEmail = true;
	} else {
		$("#customer_register_error").html("");
		$("#customer_email").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
		errorCustomerEmail = false;
	}
	return true;
}

function checkCustomerAgreement() {	
	if (!customerProtocolChecked) {
		$("#customer_register_error").html("请阅读并同意注册协议<br/>");
	} else {
		$("#customer_register_error").html("");
	}
	return customerProtocolChecked;
}